package com.zz.parkingsystem.controller.admin;

import com.zz.parkingsystem.aop.LogAnnotation;
import com.zz.parkingsystem.dto.ExceptionOrderDTO;
import com.zz.parkingsystem.dto.OrderDTO;
import com.zz.parkingsystem.dto.RefundOrderDTO;
import com.zz.parkingsystem.entity.Order;
import com.zz.parkingsystem.service.PCOrderService;
import com.zz.parkingsystem.util.ObjectExcelView;
import com.zz.parkingsystem.util.PageData;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import com.zz.parkingsystem.vo.ExceptionOrderVO;
import com.zz.parkingsystem.vo.OrderVO;
import com.zz.parkingsystem.vo.RefundOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/order")
@Api(tags = "PC订单管理")
public class AdminOrderController {
    @Resource
    private PCOrderService orderService;

    /**
     * -----------------------------------------------订单管理------------------------------------------
     */
    @PostMapping("/queryAllOrder")
    @ApiOperation(value = "查询所有订单", notes = "可以根据订单流水号模糊查询、日期查询；分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每一页显示的条数", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "从那一页开始", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orderId", value = "订单ID", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "oiNo", value = "订单流水号", required = false, paramType = "query"),
            @ApiImplicitParam(name = "dateRange", value = "日期范围", required = false, paramType = "query"),
    })
    public Result queryAllOrder(@RequestBody OrderDTO orderDTO) {
        orderDTO.setPageNum((orderDTO.getCurrentPage() - 1) * orderDTO.getPageSize());
        List<OrderVO> orderVOS = orderService.queryAllOrder(orderDTO);
        Long total = orderService.getOrderCount(orderDTO);
        Map map = new HashMap<>();
        map.put("orderVOS", orderVOS);
        map.put("total", total);
        return ResultGenerator.genSuccessResult(map);
    }

    @LogAnnotation(module="订单管理",operation = "更改订单状态")
    @PutMapping("/updateOrderStatus")
    @ApiOperation(value = "更改订单状态为2:已付款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单ID",required = true,dataType = "Integer"),
    })
    public Result updateOrderStatus(@RequestBody Order order) {
        String result = orderService.updateOrderStatus(new Long(order.getOrderId()));
        if ("操作成功".equals(result)) {
            return ResultGenerator.genSuccessResult(result);
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * ------------------------------------------------------异常订单------------------------------------------------------
     */
    @PostMapping("/queryAllExcOrder")
    @ApiOperation(value = "查询所有异常订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每一页显示的条数", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "从那一页开始", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "oiNo", value = "订单流水号", required = false, paramType = "query"),
            @ApiImplicitParam(name = "dateRange", value = "日期范围", required = false, paramType = "query"),
            @ApiImplicitParam(name = "eoStatus", value = "异常订单状态", required = false, paramType = "query",dataType = "Integer"),
    })
    public Result queryAllExcOrder(@RequestBody ExceptionOrderDTO exceptionOrderDTO) {
        exceptionOrderDTO.setPageNum((exceptionOrderDTO.getCurrentPage() - 1) * exceptionOrderDTO.getPageSize());
        List<ExceptionOrderVO> exceptionOrderVOS = orderService.queryAllExceptionOrder(exceptionOrderDTO);
        Long total = orderService.getExceptionCount(exceptionOrderDTO);
        Map map = new HashMap();
        map.put("exceptionOrderVOS", exceptionOrderVOS);
        map.put("total", total);
        return ResultGenerator.genSuccessResult(map);
    }

    @PutMapping("/updateEOStatus")
    @ApiOperation(value = "更改异常订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eoStatus",value = "异常订单状态，1：已处理，2：不通过",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "auId",value = "未支付订单表主键",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "orderId",value = "订单表主键",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "resetAmount",value = "修改金额",required = false,dataType = "BigDecimal"),
            @ApiImplicitParam(name = "oiNo",value = "订单流水号",required = true,paramType = "query"),
    })
    public Result updateEOStatus(@RequestBody ExceptionOrderDTO exceptionOrderDTO) {
        String result = orderService.updateExceptionStatus(exceptionOrderDTO);
        if ("操作成功".equals(result)) {
            return ResultGenerator.genSuccessResult(result);
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * -----------------------------------------------------售后订单-------------------------------------------
     */
    @PostMapping("/queryAllRefOrder")
    @ApiOperation(value = "查询所有售后订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每一页显示的条数", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "从那一页开始", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "oiNo", value = "订单流水号", required = false, paramType = "query"),
            @ApiImplicitParam(name = "dateRange", value = "日期范围", required = false, paramType = "query"),
            @ApiImplicitParam(name = "refStatus", value = "售后订单状态", required = false, paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "refId", value = "售后订单ID", required = false, paramType = "query",dataType = "Integer"),
    })
    public Result queryAllRefOrder(@RequestBody RefundOrderDTO refundOrderDTO) {
        refundOrderDTO.setPageNum((refundOrderDTO.getCurrentPage() - 1) * refundOrderDTO.getPageSize());
        List<RefundOrderVO> refundOrderVOS = orderService.queryAllRefundOrder(refundOrderDTO);
        Long total = orderService.getRefundCount(refundOrderDTO);
        Map map = new HashMap();
        map.put("refundOrderVOS", refundOrderVOS);
        map.put("total", total);
        return ResultGenerator.genSuccessResult(map);
    }

    @PutMapping("/updateRefStatus")
    @ApiOperation(value = "更改售后订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "refStatus",value = "售后订单状态，1：已处理，2：不通过",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "refId", value = "售后订单ID", required = true, paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "apId", value = "支付申诉表主键", required = true, paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "orderId", value = "订单表主键", required = true, paramType = "query",dataType = "Integer"),
    })
    public Result updateRefStatus(@RequestBody RefundOrderDTO refundOrderDTO) {
        String result = orderService.updateRefundStatus(refundOrderDTO);
        if ("操作成功".equals(result)) {
            return ResultGenerator.genSuccessResult(result);
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * ---------------------------------------------------------导出Excel----------------------------------------------------
     */
    @GetMapping(value = "/excelAllOrder")
    public ModelAndView exportOrderExcel() {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        OrderDTO orderDTO = new OrderDTO();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<String> titles = new ArrayList<>();
            titles.add("订单编号"); // 1
            titles.add("提交时间"); // 2
            titles.add("车牌号码"); // 3
            titles.add("所属路段"); // 4
            titles.add("泊位编号"); // 5
            titles.add("巡检员"); // 6
            titles.add("订单金额"); // 7
            titles.add("订单状态"); // 8
            dataMap.put("titles", titles);
            List<OrderVO> orderVOS = orderService.queryAllOrderExcel(orderDTO);
            List<PageData> varList = new ArrayList<>();
            for (int i = 0; i < orderVOS.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", orderVOS.get(i).getOiNo()); // 1
                vpd.put("var2", orderVOS.get(i).getOiStartTime()); // 2
                vpd.put("var3", orderVOS.get(i).getCarLicensePlate()); // 3
                vpd.put("var4", orderVOS.get(i).getSecName()); // 4
                vpd.put("var5", orderVOS.get(i).getPkNo()); // 5
                vpd.put("var6", orderVOS.get(i).getInsNickname()); // 6
                vpd.put("var7", orderVOS.get(i).getOiAmount()); // 7
                vpd.put("var8", orderVOS.get(i).getOrderStatus()); // 8
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    @GetMapping(value = "/excelAllExceptionOrder")
    public ModelAndView exportExceptionOrderExcel() {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        ExceptionOrderDTO exceptionOrderDTO = new ExceptionOrderDTO();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<String> titles = new ArrayList<>();
            titles.add("订单编号"); // 1
            titles.add("提交时间"); // 2
            titles.add("车牌号码"); // 3
            titles.add("所属路段"); // 4
            titles.add("泊位编号"); // 5
            titles.add("巡检员"); // 6
            titles.add("订单金额"); // 7
            titles.add("售后状态"); // 8
            dataMap.put("titles", titles);
            List<ExceptionOrderVO> exceptionOrderVOS = orderService.queryAllExceptionOrderExcel(exceptionOrderDTO);
            List<PageData> varList = new ArrayList<>();
            for (int i = 0; i < exceptionOrderVOS.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", exceptionOrderVOS.get(i).getOiNo()); // 1
                vpd.put("var2", exceptionOrderVOS.get(i).getOiStartTime()); // 2
                vpd.put("var3", exceptionOrderVOS.get(i).getCarLicensePlate()); // 3
                vpd.put("var4", exceptionOrderVOS.get(i).getSecName()); // 4
                vpd.put("var5", exceptionOrderVOS.get(i).getPkNo()); // 5
                vpd.put("var6", exceptionOrderVOS.get(i).getInsNickname()); // 6
                vpd.put("var7", exceptionOrderVOS.get(i).getOiAmount()); // 7
                vpd.put("var8", exceptionOrderVOS.get(i).getEoStatus()); // 8
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    @GetMapping(value = "/excelAllRefundOrder")
    public ModelAndView exportRefundOrderExcel() {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        RefundOrderDTO refundOrderDTO = new RefundOrderDTO();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<String> titles = new ArrayList<>();
            titles.add("订单编号"); // 1
            titles.add("提交时间"); // 2
            titles.add("车牌号码"); // 3
            titles.add("所属路段"); // 4
            titles.add("泊位编号"); // 5
            titles.add("巡检员"); // 6
            titles.add("订单金额"); // 7
            titles.add("售后状态"); // 8
            dataMap.put("titles", titles);
            List<RefundOrderVO> refundOrderVOS = orderService.queryAllRefundOrderExcel(refundOrderDTO);
            List<PageData> varList = new ArrayList<>();
            for (int i = 0; i < refundOrderVOS.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", refundOrderVOS.get(i).getOiNo()); // 1
                vpd.put("var2", refundOrderVOS.get(i).getOiStartTime()); // 2
                vpd.put("var3", refundOrderVOS.get(i).getCarLicensePlate()); // 3
                vpd.put("var4", refundOrderVOS.get(i).getSecName()); // 4
                vpd.put("var5", refundOrderVOS.get(i).getPkNo()); // 5
                vpd.put("var6", refundOrderVOS.get(i).getInsNickname()); // 6
                vpd.put("var7", refundOrderVOS.get(i).getOiAmount()); // 7
                vpd.put("var8", refundOrderVOS.get(i).getRefStatus()); // 8
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
}
